package java02;

import java.util.Arrays;
import java.util.Scanner;

public class Java0203 {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		String []slist = new String[n];
		String []birth1 = new String[n];
		String []birth2 = new String[n];
		String s = "";
		String b = "";
		//ArrayList<String>slist=new ArrayList<String>();
		for(int i=0;i<n;i++){
			slist[i]=in.next();
			birth1[i]=getBirthDate(slist[i]);
			birth2[i]=getBirthDate(slist[i]);
		}
		while(in.hasNext()){
			String str=in.next();
			if(str.equals("sort1")){
				Arrays.sort(birth1);
				for(int i=0;i<n;i++){
					System.out.println(birth1[i]);
				}
			}
			else if( str.equals("sort2")){
				for(int i=0;i<n;i++){
					for(int j=i;j<n;j++)
					{
						if(birth2[i].compareTo(birth2[j])>0)
						{
						s=slist[i];slist[i]=slist[j];slist[j]=s;
						b=birth2[i];birth2[i]=birth2[j];birth2[j]=b;
						}
					}
				}
				for(int k=0;k<n;k++){
					System.out.println(slist[k]);
				}
			}
	
			else
				System.out.println("exit");
			}
		}
	public static String getBirthDate(String id)
	{
		String year=id.substring(6,10);
		String month=id.substring(10,12);
		String day=id.substring(12,14);
		return year+"-"+month+"-"+day;
	}
}
